package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.DateControl;
import net.tycmc.cty.ctyms.timejob.common.DateList;
import net.tycmc.cty.ctyms.timejob.common.EquipVersionCommon;
import net.tycmc.cty.ctyms.timejob.common.LogUtil;
import net.tycmc.cty.ctyms.timejob.common.MapUtils;
import net.tycmc.cty.ctyms.timejob.common.SMSContentCfg;
import net.tycmc.cty.ctyms.timejob.iems.dao.IOFenTipDao;
import net.tycmc.cty.ctyms.timejob.iems.dao.SMSInfoDAO;

/**
 * 进出围栏提示
 *@author jinjiayu20141015
 *@remark 5分钟执行一次
 */
public class IOFenTipServcie {
	
	private IOFenTipDao ioFenTipDao;
	private SMSInfoDAO smsInfoDAO;

	public IOFenTipDao getIoFenTipDao() {
		return ioFenTipDao;
	}

	public void setIoFenTipDao(IOFenTipDao ioFenTipDao) {
		this.ioFenTipDao = ioFenTipDao;
	}

	public SMSInfoDAO getSmsInfoDAO() {
		return smsInfoDAO;
	}

	public void setSmsInfoDAO(SMSInfoDAO smsInfoDAO) {
		this.smsInfoDAO = smsInfoDAO;
	}

	private static boolean ISSUCCESS = false;
	
	public void IOFenTip() {
		
		String termSoftVer414 = EquipVersionCommon.getSoftVerByAgrType("Vcl_JPP");
		
		List<Map<String,Object>> VFs = ioFenTipDao.getVFs(termSoftVer414);
		
		List<Map<String,Object>> VD = ioFenTipDao.getVD(termSoftVer414);
		
		Map<String,List<Map<String,Object>>> VS = ioFenTipDao.getVS(termSoftVer414);
		
		List<String> sqlList = new ArrayList<String>();				//存放sql
		List<Object[]> paramList = new ArrayList<Object[]>();		//存放参数
		
		String isOpen = null;
		String vclid = null;			//设备id
		String fenid = null;			//围栏id
		String gpstime = null;			//gps时间
		String inFen = null;			//1为进围栏，0为出围栏
		String SMSContent = null;		//短信内容
		String SMSContentPro = null;	//提示中心提示内容
		Map<String,Object> map2 = null;	//辅助map
		List<Map<String,Object>> list2 = null;
		List<Map<String,Object>> list3 = null;
		Map<String,Object> map22 = new HashMap<String, Object>();
		
		/******************位置点获取BEGIN*********************************/
		List<String[]> poi = new ArrayList<String[]>();
		for(Map<String,Object> ll : VFs){
			poi.add(new String[]{MapGetter.getString(ll, "FenO_FenceID"),MapGetter.getString(ll, "VclPL_Lo"),MapGetter.getString(ll, "VclPL_La")});
		}
		List<String[]> poi2 = null;
		if(poi.size()>=1){
			poi2 = MapUtils.getPositionPromt(poi,"zh_CN");
		}
		if(null != poi2){
			for(int i=0;i<VFs.size();i++){
				if(0 == poi2.size()) break;
				for(int j=0;j<poi2.size();j++){
					if(MapGetter.getString(VFs.get(i), "FenO_FenceID").equals(poi2.get(j)[0])){
						VFs.get(i).put("Position",poi2.get(j)[1]);
						poi2.remove(j);
						break;
					}
				}
			}
		}
		/******************位置点获取END*********************************/
		
		List<Map<String,Object>> userList = smsInfoDAO.getSMSUsers_IOFenTip();
		// 获取配置的发送时间
		List<String> smsSendTimeList = smsInfoDAO.getSMSCfgSendTime("16");
		for(Map<String,Object> map_VFs : VFs){
			vclid = MapGetter.getString(map_VFs, "VehicleID");
			fenid = MapGetter.getString(map_VFs, "FenO_FenceID");
			gpstime = MapGetter.getString(map_VFs, "VclPL_PstnTime");
			inFen = MapGetter.getString(map_VFs, "infen");
			isOpen = MapGetter.getString(map_VFs, "VclPS_isOpen");
			if(!"0".equals(isOpen)){
				if(null != VS){
					if(VS.containsKey(vclid)){
						list2 = VS.get(vclid);
						Map<String,Object> map_lsit2 = new HashMap<String, Object>();
						for(int i=0;i<=list2.size();i++){
							if(i == list2.size()){				//设备目前处于多围栏且当前围栏不在围栏最新表内
								//VS集合中无该设备与该围栏的信息
								if("1".equals(inFen)){
									//设备最新位置在围栏内
									for(Map<String,Object> map_VD : VD){
										if(MapGetter.getString(map_VD, "MsgTL_Vcl_ID").equals(vclid)){
											map2 = map_VD;			//获取设备里程油耗信息等
											break;
										}
									}
									SMSContent = SMSContentCfg.SMSCCfgServiceInFenTip.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
											replace("X_Time", DateList.getFormatDate(gpstime, 2).substring(5, 20)).replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
									SMSContentPro = SMSContentCfg.SMSCCfgServiceInFenTipPro.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
											replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
									//进围栏报警
									ioFenTipDao.insertPro(vclid,fenid,gpstime,"1",SMSContent,SMSContentPro,map2,MapGetter.getString(map_VFs, "VclPL_Lo"),MapGetter.getString(map_VFs, "VclPL_La"),MapGetter.getString(map_VFs, "Position"),MapGetter.getString(map_VFs, "VclPL_MsgID"),MapGetter.getString(map_VFs, "VclPL_MsgTp_ID"),sqlList,paramList);
									//向VS中增加数据及表中增加的数据
									list2 = VS.get(vclid);
									map22 = new HashMap<String, Object>();
									map22.put("VclFSL_VclID", vclid);
									map22.put("VclFSL_FenceID", fenid);
									list2.add(map22);
									VS.put(vclid, list2);
									
									ioFenTipDao.insertFenLast(vclid,fenid,gpstime,sqlList,paramList);
									//发送短信给相关人员
									ioFenTipDao.IOSMSInfo(sqlList, paramList, userList, smsSendTimeList,SMSContent,MapGetter.getString(map_VFs, "dbcode"),MapGetter.getString(map_VFs, "OrgFen_OrgCode"),gpstime,vclid,"1");
									break;
								}else{
									//设备最新位置在围栏外
									break;
								}
							}
								
							map_lsit2 = list2.get(i);
							if(fenid.equals(MapGetter.getString(map_lsit2, "VclFSL_FenceID"))){
								//VS集合中有该设备与该围栏的信息
								if("1".equals(inFen)){
									//设备最新位置在围栏内
									break;
								}else{
									//设备最新位置在围栏外
									for(Map<String,Object> map_VD : VD){
										if(MapGetter.getString(map_VD, "MsgTL_Vcl_ID").equals(vclid)){
											map2 = map_VD;
											break;
										}
									}
									SMSContent = SMSContentCfg.SMSCCfgServiceOutFenTip.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
											replace("X_Time", DateList.getFormatDate(gpstime, 2).substring(5, 20)).replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
									SMSContentPro = SMSContentCfg.SMSCCfgServiceOutFenTipPro.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
											replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
									//出围栏报警
									ioFenTipDao.insertPro(vclid,fenid,gpstime,"2",SMSContent,SMSContentPro,map2,MapGetter.getString(map_VFs, "VclPL_Lo"),MapGetter.getString(map_VFs, "VclPL_La"),MapGetter.getString(map_VFs, "Position"),MapGetter.getString(map_VFs, "VclPL_MsgID"),MapGetter.getString(map_VFs, "VclPL_MsgTp_ID"),sqlList,paramList);
									//删除VS中的数据及表中的数据
									list3 = VS.get(vclid);
									for(Map<String,Object> map_list3 :list3){
										if(MapGetter.getString(map_list3, "VclFSL_FenceID").equals(fenid)){
											list3.remove(map_list3);
											break;
										}
									}
									
									ioFenTipDao.deleteVS(vclid,fenid,sqlList,paramList);
									//发送短信给相关人员
									ioFenTipDao.IOSMSInfo(sqlList, paramList, userList, smsSendTimeList,SMSContent,MapGetter.getString(map_VFs, "dbcode"),MapGetter.getString(map_VFs, "OrgFen_OrgCode"),gpstime,vclid,"2");
								}
							}
						}
					}else{
						//VS集合中无该设备与该围栏的信息
						if("1".equals(inFen)){
							//设备最新位置在围栏内
							for(Map<String,Object> map_VD : VD){
								if(MapGetter.getString(map_VD, "MsgTL_Vcl_ID").equals(vclid)){
									map2 = map_VD;
									break;
								}
							}
							SMSContent = SMSContentCfg.SMSCCfgServiceInFenTip.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
									replace("X_Time", DateList.getFormatDate(gpstime, 2).substring(5, 20)).replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
							SMSContentPro = SMSContentCfg.SMSCCfgServiceInFenTipPro.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
									replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
							//进围栏报警
							ioFenTipDao.insertPro(vclid,fenid,gpstime,"1",SMSContent,SMSContentPro,map2,MapGetter.getString(map_VFs, "VclPL_Lo"),MapGetter.getString(map_VFs, "VclPL_La"),MapGetter.getString(map_VFs, "Position"),MapGetter.getString(map_VFs, "VclPL_MsgID"),MapGetter.getString(map_VFs, "VclPL_MsgTp_ID"),sqlList,paramList);
							//向VS中的增加数据及表中的数据
							list2 = new ArrayList<Map<String,Object>>();
							map22 = new HashMap<String, Object>();
							map22.put("VclFSL_VclID", vclid);
							map22.put("VclFSL_FenceID", fenid);
							list2.add(map22);
							VS.put(vclid, list2);
							
							ioFenTipDao.insertFenLast(vclid,fenid,gpstime,sqlList,paramList);
							//发送短信给相关人员
							ioFenTipDao.IOSMSInfo(sqlList, paramList, userList, smsSendTimeList,SMSContent,MapGetter.getString(map_VFs, "dbcode"),MapGetter.getString(map_VFs, "OrgFen_OrgCode"),gpstime,vclid,"1");
						}else{
							//设备最新位置在围栏外
							continue;
						}
					}
				}else{
					//VS集合中无该设备与该围栏的信息
					if("1".equals(inFen)){
						//设备最新位置在围栏内
						for(Map<String,Object> map_VD : VD){
							if(MapGetter.getString(map_VD, "MsgTL_Vcl_ID").equals(vclid)){
								map2 = map_VD;
								break;
							}
						}
						SMSContent = SMSContentCfg.SMSCCfgServiceInFenTip.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
								replace("X_Time", DateList.getFormatDate(gpstime, 2).substring(5,20)).replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
						SMSContentPro = SMSContentCfg.SMSCCfgServiceInFenTipPro.replace("X_VclNum", MapGetter.getString(map_VFs, "VehicleNumber")).
								replace("X_Fen", MapGetter.getString(map_VFs, "FenceName"));
						//进围栏报警
						ioFenTipDao.insertPro(vclid,fenid,gpstime,"1",SMSContent,SMSContentPro,map2,MapGetter.getString(map_VFs, "VclPL_Lo"),MapGetter.getString(map_VFs, "VclPL_La"),MapGetter.getString(map_VFs, "Position"),MapGetter.getString(map_VFs, "VclPL_MsgID"),MapGetter.getString(map_VFs, "VclPL_MsgTp_ID"),sqlList,paramList);
						//向VS中的增加数据及表中的数据
						list2 = new ArrayList<Map<String,Object>>();
						map22 = new HashMap<String, Object>();
						map22.put("VclFSL_VclID", vclid);
						map22.put("VclFSL_FenceID", fenid);
						list2.add(map22);
						if(null == VS)
							VS = new HashMap<String, List<Map<String,Object>>>();
						VS.put(vclid, list2);
						
						ioFenTipDao.insertFenLast(vclid,fenid,gpstime,sqlList,paramList);
						//发送短信给相关人员
						ioFenTipDao.IOSMSInfo(sqlList, paramList, userList, smsSendTimeList,SMSContent,MapGetter.getString(map_VFs, "dbcode"),MapGetter.getString(map_VFs, "OrgFen_OrgCode"),gpstime,vclid,"1");
					}else{
						//设备最新位置在围栏外
						continue;
					}
				}
			}
			if(sqlList.size()>0){
				boolean flag = ioFenTipDao.tranSQLs(sqlList, paramList);
				if(!flag){
					LogUtil.info(this.getClass().getName(), "进出围栏提示统计作业，设备id:"+vclid+";围栏id:"+fenid+",插入信息失败！");
				}
				sqlList = new ArrayList<String>();				
				paramList = new ArrayList<Object[]>();		
			}
		}
		ISSUCCESS = true;
	}

	public void EFFTip() {
		if(!ISSUCCESS) return ;			//true说明进出围栏提示成功执行
		String termSoftVer414 = EquipVersionCommon.getSoftVerByAgrType("Vcl_JPP");
		//获取最新位置在时效围栏外的设备集合
		List<Map<String,Object>> Vcls = ioFenTipDao.getVcls(termSoftVer414);
		String vclid = null;			//设备id
		Map<String,Object> map2 = null;	//辅助map
		
		//去除在围栏内的设备信息，时效预警的设备必须在全部围栏外
		StringBuffer vclids = new StringBuffer("");
		for(int i=0;i<Vcls.size();i++){
			map2 = Vcls.get(i);
			vclid = MapGetter.getString(map2, "vclpl_vcl_id");
			if("1".equals(MapGetter.getString(map2, "infen"))){
				vclids.append(",'"+vclid+"'");
			}
		}
		vclids.append(",");
		if(vclids.length()>1){
			for(int i=Vcls.size()-1;i>=0;i--){
				vclid = MapGetter.getString(Vcls.get(i), "vclpl_vcl_id");
				if(vclids.toString().contains(",'"+vclid+"',")){
					Vcls.remove(i);
				}
			}
		}
		
		/******************位置点获取BEGIN*********************************/
		List<String[]> poi = new ArrayList<String[]>();
		for(Map<String,Object> ll : Vcls){
			poi.add(new String[]{MapGetter.getString(ll, "VclPL_ID"),MapGetter.getString(ll, "VclPL_Lo"),MapGetter.getString(ll, "VclPL_La")});
		}
		List<String[]> poi2 = null;
		if(poi.size()>=1){
			poi2 = MapUtils.getPositionPromt(poi,"zh_CN");
		}
		String position = null;
		if(null != poi2){
			for(int i=0;i<Vcls.size();i++){
				if(0 == poi2.size()) break;
				for(int j=0;j<poi2.size();j++){
					if(MapGetter.getString(Vcls.get(i), "VclPL_ID").equals(poi2.get(j)[0])){
						position = poi2.get(j)[1].replaceAll("省", "").replaceAll("市", "").replaceAll("附近", "");
						Vcls.get(i).put("Position",position);
						poi2.remove(j);
						break;
					}
				}
			}
		}
		/******************位置点获取END*********************************/
		//从提示中心获取设备最后一次出围栏提示信息集合
		Map<String,List<Map<String,Object>>> Promts = ioFenTipDao.getPromts(termSoftVer414);
		//从提示中心获取设备最后一次出围栏提示信息集合
		Map<String,Object> Promts_pro = ioFenTipDao.getPromtsP(termSoftVer414);
		//从提示中心获取关机提醒未接触的集合
		Map<String,Object> EngOff = ioFenTipDao.getEngOff(termSoftVer414);
		//短信发送人员
		List<Map<String,Object>> userList = smsInfoDAO.getSMSUsers_Eff();
		// 获取配置的发送时间
		List<String> smsSendTimeList = smsInfoDAO.getSMSCfgSendTime("17");
		//从定时参数表获取发动机状态
		Map<String,Object> msgEgnState = ioFenTipDao.getMsgEgnState();
		
		String fenid = "";				//出围栏id
		String isOpen = null;			//是否开启提示开关
		String prompt_time = null;		//出围栏时间
		String promptP_time = null;		//时效预警时间
		String efftime = null;			//关机持续时间
		String egnstate = null;			//发动机状态
		int slv = 0;					//车速低提醒值
		int slt = 0;					//车速低持续时间
		int sdt = 0;					//关机持续时间
		long miaoshu = 0;				//时间间隔
		String SMSContent = null;		//短信内容
		String SMSContentPro = null;	//提示中心提示内容
		List<String> sqlList = new ArrayList<String>();				//存放sql
		List<Object[]> paramList = new ArrayList<Object[]>();		//存放参数
		List<String> egnon = new ArrayList<String>();				//开机提示集合，防止重复报警
		List<String> egnoff = new ArrayList<String>();				//关机提示集合，防止重复报警
		List<Map<String, Object>> pro_list = new ArrayList<Map<String, Object>>();	
		for(Map<String,Object> map_Vcls : Vcls){
			vclid = MapGetter.getString(map_Vcls, "vclpl_vcl_id");
			fenid = MapGetter.getString(map_Vcls, "fenceid");
			isOpen = MapGetter.getString(map_Vcls, "VclPS_isOpen");
			if(!"0".equals(isOpen)){
				if(null != Promts){
					if(Promts.containsKey(vclid)){
						//设备在围栏外
						egnstate = MapGetter.getString(msgEgnState, vclid);
						if("1".equals(egnstate)){
							//开机
							if(EngOff.containsKey(vclid) && !egnon.contains(vclid)){
								egnon.add(vclid);
								miaoshu = DateControl.getMiaoShu(MapGetter.getString(EngOff, vclid), MapGetter.getString(map_Vcls, "VclPL_PstnTime"));
								//解除设备关机提醒并发送开机提醒
								ioFenTipDao.updateProType(vclid,"",MapGetter.getString(map_Vcls, "VclPL_PstnTime"),String.valueOf(miaoshu/60),sqlList,paramList);
								SMSContent = SMSContentCfg.SMSCCfgServiceOpenSMS.replace("X_VclNum", MapGetter.getString(map_Vcls, "VehicleNumber")).
										replace("X_Mmm", String.valueOf(miaoshu/60)).replace("XXXX", MapGetter.getString(map_Vcls, "Position"));
								ioFenTipDao.effSMS(sqlList, paramList, userList,MapGetter.getString(map_Vcls, "VclPL_PstnTime"),vclid,"2","3", smsSendTimeList,SMSContent);
							}else{
								//
							}
							//只有出围栏提示表中有数据才能产生时效预警
							pro_list = Promts.get(vclid);
							for(Map<String, Object> map_P : pro_list){
								if(fenid.equals(MapGetter.getString(map_P, "PassFT_FenceID"))){
									slt = MapGetter.getInt(map_Vcls, "EffC_SpeedLowTime");
									slv = MapGetter.getInt(map_Vcls, "EffC_SpeedLowValue");
									efftime = MapGetter.getString(map_P, "PassFT_MsgTime");
									miaoshu = DateControl.getMiaoShu(efftime, MapGetter.getString(map_Vcls, "VclPL_PstnTime"));
									
									if(miaoshu  <= slt*60){
										if(sqlList.size()>0){
											boolean flag = ioFenTipDao.tranSQLs(sqlList, paramList);
											if(!flag){
												LogUtil.info(this.getClass().getName(), "时效预警统计作业，设备id:"+vclid+",插入信息失败！");
											}
											sqlList = new ArrayList<String>();				
											paramList = new ArrayList<Object[]>();
										}
										sqlList = new ArrayList<String>();				
										paramList = new ArrayList<Object[]>();
										continue;
									}else{
										int count = ioFenTipDao.lowEffTip(vclid,MapGetter.getString(map_Vcls, "VclPL_PstnTime"),slt,slv);
										if(count > 0){
											//
										}else{
											prompt_time = MapGetter.getString(map_P, "PassFT_MsgTime");			//该设备最后一次出围栏的时间
											promptP_time = MapGetter.getString(Promts_pro, vclid);				//该设备上次车速低提醒的时间
											if(StringUtil.isNull(promptP_time)){
												long chazhi = DateControl.getMiaoShu(promptP_time, prompt_time);
												if(chazhi<0){
													if(sqlList.size()>0){
														boolean flag = ioFenTipDao.tranSQLs(sqlList, paramList);
														if(!flag){
															LogUtil.info(this.getClass().getName(), "时效预警统计作业，设备id:"+vclid+",插入信息失败！");
														}
														sqlList = new ArrayList<String>();				
														paramList = new ArrayList<Object[]>();
													}
													sqlList = new ArrayList<String>();				
													paramList = new ArrayList<Object[]>();
													continue;
												}
											}
											//车速低提醒
											SMSContentPro = SMSContentCfg.SMSCCfgServiceLowPro.replace("X_VclNum", MapGetter.getString(map_Vcls, "VehicleNumber")).
													replace("X_Speed",String.valueOf(slv)).replace("X_Mmm", String.valueOf(slt));
											ioFenTipDao.closeAndLowTip(map_Vcls,SMSContentPro,"1","","1",sqlList,paramList);
											SMSContent = SMSContentCfg.SMSCCfgServiceLowSMS.replace("X_VclNum", MapGetter.getString(map_Vcls, "VehicleNumber")).
													replace("X_Speed",String.valueOf(slv)).
													replace("X_Mmm", String.valueOf(slt)).replace("XXXX", MapGetter.getString(map_Vcls, "Position"));
											ioFenTipDao.effSMS(sqlList, paramList, userList,MapGetter.getString(map_Vcls, "VclPL_PstnTime"),vclid,"1","1", smsSendTimeList,SMSContent);
										}
									}
									break;
								}
							}
						}else if("0".equals(egnstate)){
							//关机
							sdt = MapGetter.getInt(map_Vcls, "EffC_ShutDownTime");
							
							int countOpen = ioFenTipDao.countOpen(vclid,sdt,MapGetter.getString(map_Vcls, "VclPL_PstnTime"));
							if(countOpen>0){
								continue;
							}else{
								if(EngOff.containsKey(vclid) || egnoff.contains(vclid)){
									//
								}else{
									egnoff.add(vclid);
									
									SMSContentPro = SMSContentCfg.SMSCCfgServiceColsePro.replace("X_VclNum", MapGetter.getString(map_Vcls, "VehicleNumber")).replace("X_Mmm", String.valueOf(sdt));
									ioFenTipDao.closeAndLowTip(map_Vcls,SMSContentPro,"2","","1",sqlList,paramList);
									//发送短信
									SMSContent = SMSContentCfg.SMSCCfgServiceCloseSMS.replace("X_VclNum", MapGetter.getString(map_Vcls, "VehicleNumber")).
											replace("X_Mmm", String.valueOf(sdt)).replace("XXXX", MapGetter.getString(map_Vcls, "Position"));
									ioFenTipDao.effSMS(sqlList, paramList, userList,MapGetter.getString(map_Vcls, "VclPL_PstnTime"),vclid,"2","1", smsSendTimeList,SMSContent);
								}
							}
						}else{
							//
						}
					}else{
						//设备在围栏内
						continue;
					}
				}
			}
			if(sqlList.size()>0){
				boolean flag = ioFenTipDao.tranSQLs(sqlList, paramList);
				if(!flag){
					LogUtil.info(this.getClass().getName(), "时效预警统计作业，设备id:"+vclid+",插入信息失败！");
				}
				sqlList = new ArrayList<String>();				
				paramList = new ArrayList<Object[]>();
			}
		}
		ISSUCCESS = false;
	}
}